// src/stores/template.js
import { defineStore } from 'pinia'
import {
    getTemplatePage,
    addTemplate,
    updateTemplate,
    deleteTemplate,
    toggleTemplateStatus,
    batchDeleteTemplates,
    batchToggleStatus
} from '@/api/template'

export const useTemplateStore = defineStore('template', {
    state: () => ({
        templates: [],
        loading: false,
        error: null
    }),

    actions: {
        async batchDeleteTemplates(ids) {
            try {
                await batchDeleteTemplates(ids)  // 直接传递数组
            } catch (error) {
                throw error
            }
        },

        async batchToggleStatus(ids, enabled) {
            try {
                await batchToggleStatus(ids, enabled)
            } catch (error) {
                throw error
            }
        },

        async toggleTemplateStatus(id, enabled) {
            try {
                await toggleTemplateStatus(id, enabled)
            } catch (error) {
                throw error
            }
        },

        async deleteTemplate(id) {
            try {
                await deleteTemplate(id)
            } catch (error) {
                throw error
            }
        },

        async getTemplateList(params) {
            try {
                const response = await getTemplatePage(params)
                return response.data
            } catch (error) {
                throw error
            }
        },

        async addTemplate(template) {
            try {
                await addTemplate(template)
            } catch (error) {
                throw error
            }
        },

        async updateTemplate(template) {
            try {
                await updateTemplate(template.id, template)
            } catch (error) {
                throw error
            }
        },
    }
})